package info.openlvb.openface.server.ldap;

/*
ESUP-portail is a french academic project developed under the GPL (General Public License) augmented according to the following :
A binary or source file developped by ESUP-portail can be used or compiled with products under Apache license.
The official english text of the GPL can be found here : http://www.gnu.org/licenses/gpl.html .
A non official french translation can be found here : http://www.linux-France.org/article/these/gpl.html .
The different kinds of licenses governing the products developed by the Apache foundation can be found here : http://www.apache.org/licenses .
It follows that you can as well as use download contents as well modify and redistribute them provided you respect the GPL terms.
Downloading and using such contents do not provide any guaranty.
Be sure that you have well understood the terms of the license before using the contents it covers.
The ESUP-portail distribution includes the following distributions :
    * UPortal :
      software developed by JA-SIG (Java Architecture - Special Interest Group)
      You can find the license page here : http://mis105.udel.edu/ja-sig/uportal/license.html
    * CAS :
      SSO solution developed by Yale University
      You can find the project page here : http://www.yale.edu/tp/auth
    * Cocoon :
      XML framework distributed by the Apache foundation under Apache license;
      Please find the full text here : http://cocoon.apache.org/2.1/license.html
    * Mod_dav:
      A DAV module for Apache web server
      You can find the project page here : http://www.webdav.org/mod_dav
    * IMP :
      webmail from Horde application framework
      You can find the project page here : http://www.horde.org
    * �. To be completed
*/


import java.security.*;

/** 
 * <p>Titre : CModifPerso</p>
 * <p>Description : Canal de modification des informations LDAP</p>
 * <p>Copyright : Copyright (c) 2004</p>
 * <p>Soci�t� : esupPortail</p>
 * @author Denis Elbaz
 * @version 1.0
 */

public class SmbNTPassword { 
	

	public boolean check(String value) {
		if (value==null)return false;
		if (value.equals(""))return false;
		return true;
	}

	public String transform(String password) {
		
		byte[] unicodeByteArrayPassword = new byte[(password.length()) * 2];
		byte[] byteArrayPassword = password.getBytes();
		for(int i=0; i<byteArrayPassword.length; i++){
			unicodeByteArrayPassword[2*i] = byteArrayPassword[i];
			unicodeByteArrayPassword[2*i + 1] = 0;
		}
        
		byte[] byteArrayEncodedPassword = null;
		try {
			java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
		    //MD4  
			MessageDigest messageDigest = MessageDigest.getInstance("MD4");
		      byteArrayEncodedPassword = messageDigest.digest(unicodeByteArrayPassword);
		}
		catch (Exception exc) {
		      exc.printStackTrace();
		}
	    
		StringBuffer res = new StringBuffer();
		for( int i=0; i<byteArrayEncodedPassword.length; i++ ) {
		    String hex = Integer.toHexString( byteArrayEncodedPassword[i] );
		    if( hex.length()<2 ) hex = "0" + hex;
		    res.append( hex.substring( hex.length()-2 ) );
		}
		String encodedPassword =  res.toString().toUpperCase();

	    return encodedPassword;
	}
	
}
